package fr.mairie.parc.page.technicien;

import javax.annotation.Resource;

import org.apache.click.control.Form;
import org.apache.click.control.ImageSubmit;
import org.apache.click.util.Bindable;
import org.springframework.context.ApplicationContext;

import fr.mairie.parc.context.AppContext;
import fr.mairie.parc.control.technicien.TechnicienFields;
import fr.mairie.parc.domain.Technicien;
import fr.mairie.parc.page.BorderPage;
import fr.mairie.parc.service.TechnicienManager;

public class TechnicienPage extends BorderPage{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	@Resource private TechnicienManager technicienManager;
	@Bindable protected Form form = new Form();

	protected TechnicienFields fields = new TechnicienFields();

	public TechnicienPage() {
		fields.addAll(form);
		form.add(new ImageSubmit("OK", "/assets/images/32x32/accept.png", this,"onSubmit"));
		form.add(new ImageSubmit("Cancel", "/assets/images/32x32/cancel.png",this, "onCancelClick"));

	}

	@Override
	public void onInit() {
		super.onInit();
		ApplicationContext ctx = AppContext.getApplicationContext();
		technicienManager = (TechnicienManager) ctx.getBean("technicienManager");
	}

	public boolean onSubmit() {
		String msg;
		if (form.isValid()) {
			Technicien technicien = new Technicien();
			form.copyTo(technicien);
			technicienManager.addTechnicien(technicien);
						
			form.clearValues();

			msg = "A new customer record has been created.";
			addModel("msg", msg);

		} else {
			msg = getMessage("msg-form-error", form.getError());
		}

		return true;
	}

}
